<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/12/23
 * Time: 18:03
 */

namespace app\admin\controller;

use think\Db;

class Termsofuse extends Base
{
    public function sensitive(){
        if(request()->isAjax()){
            extract(input());
            $where = [];
            if(isset($reason) && $reason != ''){
                $where['s_word'] = ['like',"%$reason%"];
            }
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::table('up_sensitive')
                ->where($where)
                ->count();
            $list = Db::table('up_sensitive')
                ->where($where)
                ->order('s_updatetime desc')
                ->page($page, $limit)
                ->select();
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        return $this->fetch();
    }

    /**
     * 删除
     */
    public function del(){
        $ids = input('param.id');
        $res = Db::table('up_sensitive')->where('s_id',$ids)->delete();
        if($res){
            return json(['code' => 200, 'data' => '', 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'data' => '', 'msg' => '删除失败']);
        }
    }

    /***
     * @return mixed
     * 添加
     */
    public function add(){
        if(request()->isAjax()){
            $name = trim(input('param.content'),',');
            $name  = trim($name,' ');
            if(empty($name)){
                return json(['code' => 100, 'data' => '', 'msg' => '不能为空']);
            }
            $where['s_word'] = ['in',$name];
            $assortment_id = Db::table('up_sensitive')->where($where)->find();
            if($assortment_id){
                return json(['code' => 100, 'data' => '', 'msg' => '有重复的敏感词了']);
            }
            $arr = explode(',',$name);
            foreach ($arr as $k => $v){
                $data[] = [
                    's_word' => $v,
                    's_red' => '<span style="color: red;">' . $v . '</span>',
                    's_time' => date('Y-m-d H:i:s'),
                    's_updatetime' => date('Y-m-d H:i:s')
                ];
            }
            $res = Db::table('up_sensitive')->insertAll($data);
            if($res){
                return json(['code' => 200, 'data' =>"", 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '添加失败']);
            }
        }

        return $this->fetch();
    }

    /***
     * 编辑
     */
    public function update(){
        if(request()->isAjax()){
            $name  = trim(input('param.title'),',');
            $name  = trim($name,' ');
            if(empty($name)){
                return json(['code' => 100, 'data' => '', 'msg' => '不能为空']);
            }
            $id = input('param.id');
            $where['s_word'] = $name;
            $where['s_id'] = array('NEQ',$id);
            $assortment_id = Db::table('up_sensitive')->where($where)->find();
            if($assortment_id){
                return json(['code' => 100, 'data' => '', 'msg' => '有重复的敏感词了']);
            }
            $data['s_word'] = $name;
            $data['s_red'] = '<span style="color: red;">' . $name . '</span>';
            $data['s_updatetime'] = date('Y-m-d H:i:s');
            $res = Db::table('up_sensitive')->where('s_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'data' =>"", 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '编辑失败']);
            }
        }
        $id = input('param.id');
        $list = Db::table('up_sensitive')->where('s_id',$id)->find();
        $this->assign('list',$list);
        return $this->fetch();
    }
    /**
     * 导入Excel
     * @return mixed
     */
    public function excelIn(){
        if(request()->file('file')){
            $file = request()->file('file');
            $info  = $file->getInfo();
            $file_path = $info['tmp_name'];
            //取出文件后缀
            $file_type = explode(".",$info['name']);
            $file_type = end($file_type);
            vendor("PHPExcel.PHPExcel.IOFactory");
            if( $file_type == 'xls' ){
                $inputFileType = 'Excel5';    //这个是读 xls的
            }else{
                $inputFileType = 'Excel2007';//这个是计xlsx的
            }
            $objReader = \PHPExcel_IOFactory::createReader($inputFileType);
            $objPHPExcel = $objReader->load($file_path);
            $objWorksheet = $objPHPExcel->getActiveSheet();
            $highestRow = $objWorksheet->getHighestRow();
//            print_r($highestRow);die;
            $time = date('Y-m-d H:i:s');
            for ($i=2;$i<=$highestRow;$i++) {
                $a = $objPHPExcel->getActiveSheet ()->getCell ("A" . $i)->getValue ();//获取A列的值
                $a = trim($a, ' ');
                if($a != ''){
                    //插入学校
                    $data = [
                        's_word' => trim($a, ' '),
                        's_red' => '<span style="color: red;">' . $a . '</span>',
                        's_time' => $time,
                        's_updatetime' => $time
                    ];
                    Db::table('up_sensitive')->insert($data);
                }
            }
            echo '/admin/Termsofuse/sensitive';
        }
        return $this->fetch('termsofuse/excelin');
    }
}